package com.openbravo.pos.payment;

import java.awt.Component;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.swing.dialog.I_EnterKeyProcesser;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.PaymentInfo;
import com.openbravo.bean.PaymentInfoStoreCard;
import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.beans.JPasswordDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JStoreValueCardFinder;
import com.openbravo.pos.util.Hashcypher;
import com.openbravo.pos.util.RoundUtils;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class JPaymentStoreCard extends javax.swing.JPanel implements JPaymentInterface,I_EnterKeyProcesser{
    
    private PaymentNotifier notifier;
    private BusinessPartner customerext;
    
    private double m_dPaid;
    private double m_dTotal;

    private CustomerStoreValueCard storeCard;
    JStoreValueCardFinder cardFinder;
    
    /** Creates new form JPaymentDebt */
    public JPaymentStoreCard(PaymentNotifier notifier,I_DataLogicSales dSales) {
        this.notifier = notifier;
        initComponents();  
        cardFinder=new JStoreValueCardFinder(dSales,m_jKeys);
        panel.add(cardFinder, java.awt.BorderLayout.CENTER);
    }
    
    public void activate(BusinessPartner customerext, double dTotal, String transID) {
    	notifier.setStatus(false, false);   
        customerext = customerext;
        m_jName.setText(null);
        m_dTotal = dTotal;
        cardFinder.reset(); 
        if (customerext != null) {         
            m_jName.setText(customerext.getName());
        }        
        m_jKeys.setEnabled(true);
        cardFinder.activate();  
    }
    public PaymentInfo executePayment() {
        return new PaymentInfoStoreCard(m_dPaid, storeCard.getId());      
    }
    
    public Component getComponent() {
        return this;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jlblMessage = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        m_jKeys = new com.openbravo.editor.JEditorKeys();
        jPanel3 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel5.setLayout(new java.awt.BorderLayout());

        jlblMessage.setBackground(javax.swing.UIManager.getDefaults().getColor("Label.background"));
        jlblMessage.setEditable(false);
        jlblMessage.setLineWrap(true);
        jlblMessage.setWrapStyleWord(true);
        jlblMessage.setFocusable(false);
        jlblMessage.setPreferredSize(new java.awt.Dimension(300, 72));
        jlblMessage.setRequestFocusEnabled(false);
        jPanel6.add(jlblMessage);

        jPanel5.add(jPanel6, java.awt.BorderLayout.SOUTH);

        add(jPanel5, java.awt.BorderLayout.CENTER);
        
        panel = new JPanel();
        jPanel5.add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout(0, 0));
        jPanel4 = new javax.swing.JPanel();
        jPanel4.setPreferredSize(new Dimension(10, 80));
        panel.add(jPanel4, BorderLayout.NORTH);
        jLabel8 = new javax.swing.JLabel();
        m_jMoneyEuros = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        m_jName = new javax.swing.JTextField();
        
                jPanel4.setLayout(null);
                
                        jLabel8.setText(AppLocal.getIntString("Label.InputCash")); // NOI18N
                        jPanel4.add(jLabel8);
                        jLabel8.setBounds(20, 20, 100, 15);
                        
                                m_jMoneyEuros.setBackground(new java.awt.Color(153, 153, 255));
                                m_jMoneyEuros.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
                                m_jMoneyEuros.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
                                m_jMoneyEuros.setOpaque(true);
                                m_jMoneyEuros.setPreferredSize(new java.awt.Dimension(150, 25));
                                jPanel4.add(m_jMoneyEuros);
                                m_jMoneyEuros.setBounds(120, 15, 150, 25);
                                
                                        jLabel3.setText(AppLocal.getIntString("label.name")); // NOI18N
                                        jPanel4.add(jLabel3);
                                        jLabel3.setBounds(20, 51, 100, 15);
                                        
                                                m_jName.setEditable(false);
                                                jPanel4.add(m_jName);
                                                m_jName.setBounds(120, 51, 200, 19);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
        jPanel1.add(m_jKeys);

        jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel1.add(jPanel3);

        jPanel2.add(jPanel1, java.awt.BorderLayout.NORTH);

        add(jPanel2, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextArea jlblMessage;
    private com.openbravo.editor.JEditorKeys m_jKeys;
    private javax.swing.JLabel m_jMoneyEuros;
    private javax.swing.JTextField m_jName;
    private JPanel panel;
    // End of variables declaration//GEN-END:variables

	@Override
	public boolean enterKeyProc() {
		m_dPaid=0;
		storeCard=cardFinder.getCard();
		if(storeCard==null){
			cardFinder.findStoreValueCardByCardNum();
			cardFinder.activate();
		   return false;
		}
		
        if(storeCard!=null){
        	if(storeCard.getState()!=CustomerStoreValueCard.STATE_UseAble){
        		MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.storeCardNoUseAble"));
                msg.show(this);
                cardFinder.reset();
                return false;
        	}
				// comprobemos la clave antes de entrar...
                String sPassword = JPasswordDialog.showEditPassword(this,
                        AppLocal.getIntString("Label.Password"));
                if (sPassword != null&&Hashcypher.authenticate(storeCard.getPassword(),sPassword)) { 
                    	double ablePayVaule=storeCard.getCurLimit()-storeCard.getBackLimit();
                        if (RoundUtils.compare(ablePayVaule, m_dTotal) < 0)  { 
                            // maximum debt exceded
                            jlblMessage.setText(AppLocal.getIntString("message.customerdebtexceded"));
                            return false;
                        } else
                            jlblMessage.setText(null); 
                        m_dPaid=RoundUtils.compare(RoundUtils.getValue(ablePayVaule), m_dTotal) >= 0?m_dTotal:ablePayVaule;
                        int iCompare = RoundUtils.compare(m_dPaid, m_dTotal);
                            // if iCompare > 0 then the payment is not valid
                        notifier.setStatus(m_dPaid > 0.0 && iCompare <= 0, iCompare == 0);
                 } else {
                        MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.BadPassword"));
                        msg.show(this);
                        cardFinder.reset();
                        cardFinder.activate();
                        return false;
                 }
            }
		  m_jMoneyEuros.setText(Formats.CURRENCY.formatValue(new Double(m_dPaid)));	 	
	      return true;
	}
}